package com.anytrek.cloudcommodity.service.impl;

import com.anytrek.cloudcommodity.service.CommodityService;
import com.anytrek.cloudcommon.entity.CommodityEntity;
import com.anytrek.cloudcommon.mapper.CommodityMapper;
import com.anytrek.cloudcommon.utils.ApiResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.datasource.EnableAutoDataSourceProxy;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ ClassName CommodityServiceImpl
 * @ Author Alex2
 * @ Date 2025/8/29 15:23
 **/

@Log4j2
@Service
@RequiredArgsConstructor
@EnableAutoDataSourceProxy
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, CommodityEntity>
        implements CommodityService {

    private static final String TAG = "Alex";

    private final CommodityMapper commodityMapper;

    @Override
    public ApiResult insertCommodity(String name, Integer price, Integer stock) {
        CommodityEntity commodity = lambdaQuery().eq(CommodityEntity::getName, name).one();
        if (commodity != null) {
            log.error("{} : {}", TAG, "该商品已存在，插入失败");
            return ApiResult.error("该商品已存在，插入失败");
        }
        CommodityEntity commodityEntity = new CommodityEntity(null, name, price, stock);
        boolean save = save(commodityEntity);
        if (!save) {
            log.error("{} : {}", TAG, "插入商品失败");
            return ApiResult.error("插入商品失败");
        }
        log.info("{} : {}", TAG, "插入商品成功");
        return ApiResult.success("插入商品成功");
    }

    /**
     * @ Author: Alex
     * @ Description: 项目实战-分布式事务-根据 commodityId 获取商品价格
     */
    @Override
    public ApiResult getPriceByCommodityId(Integer commodityId) {
        CommodityEntity commodityEntity = lambdaQuery().eq(CommodityEntity::getId, commodityId).one();
        if (commodityEntity == null) {
            log.error("{} : {}", TAG, "商品不存在");
            throw new RuntimeException("商品不存在");
        }
        Integer price = commodityEntity.getPrice();
        log.info("{} : {}", TAG, "获取商品价格成功");
        return ApiResult.success("获取商品价格成功", price);
    }

    /**
     * @ Author: Alex
     * @ Description: 项目实战-分布式事务-扣减库存
     */
    @Transactional
    @Override
    public ApiResult deductStock(Integer commodityId, Integer count) {
        CommodityEntity commodityEntity = lambdaQuery().eq(CommodityEntity::getId, commodityId).one();
        if (commodityEntity == null) {
            throw new RuntimeException("商品不存在");
        }
        if (commodityEntity.getStock() < count) {
            log.error("{} : {}", TAG, "库存不足");
            throw new RuntimeException("库存不足");
        }
        commodityEntity.setStock(commodityEntity.getStock() - count);
        int update = commodityMapper.updateById(commodityEntity);
        if (update == 0) {
            log.error("{} : {}", TAG, "扣减库存失败");
            throw new RuntimeException("扣减库存失败");
        }
        log.info("{} : {}", TAG, "扣减库存成功");
        return ApiResult.success("扣减库存成功");
    }
}
